<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Friends_model extends CI_Model {

	var $table_name = 'a_friends';

	function __construct()
	{
		parent::__construct();
	}

	/**
	 * Get all a users friends
	 *
	 * @return	object
	 */
	function get_friends($user_id)
	{
		$sql = "
				(
				SELECT
					a_friends.user_two_id AS friendId,
					a_users.username AS friendUsername
				FROM
					a_friends
				JOIN
					a_users ON a_users.id=a_friends.user_two_id
				WHERE
					a_friends.user_one_id = " . $user_id . "
				)
				
				UNION
				
				(
				SELECT
					a_friends.user_one_id AS friendId,
					a_users.username AS friendUsername
				FROM
					a_friends
				JOIN
					a_users ON a_users.id=a_friends.user_one_id
				WHERE
					a_friends.user_two_id = " . $user_id . "
				)
				
				ORDER BY
					friendUsername
				ASC
				";
		$query = $this->db->query($sql);
		return $query->result();
	}
	
	/**
	 * Get count of friends
	 *
	 * @return	object
	 */
	function get_count_friends($user_id)
	{
		$sql = "
				(
				SELECT
					a_friends.user_two_id AS friendId,
					a_users.username AS friendUsername
				FROM
					a_friends
				JOIN
					a_users ON a_users.id=a_friends.user_two_id
				WHERE
					a_friends.user_one_id = " . $user_id . "
				)
				
				UNION
				
				(
				SELECT
					a_friends.user_one_id AS friendId,
					a_users.username AS friendUsername
				FROM
					a_friends
				JOIN
					a_users ON a_users.id=a_friends.user_one_id
				WHERE
					a_friends.user_two_id = " . $user_id . "
				)
				
				ORDER BY
					friendUsername
				ASC
				";
		$query = $this->db->query($sql);
		return $query->num_rows();
	}	
 }
 ?>